// HomeCard.tsx
import { PlaneTakeoff, ShieldAlert, ShieldX, TowerControl } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import apiClient from "@/lib/request";
import { Result, TotalFlihts } from "@/lib/dto";

const fetchCardData = async () => {
  // 获取今天的日期yyy-MM-dd格式
  const today = new Date().toISOString().slice(0, 10);
  try {
    const res: Result<TotalFlihts> = await apiClient.get(
      `flight/total-flights/date/${today}`
    );
    return res.data;
  } catch (error) {
    // 抛出错误
    throw new Error("Error fetching data");
  }
};

const HomeCard = async ({ ...props }) => {
  const cardData = await fetchCardData();
  if (!cardData) {
    return <div>加载中...</div>;
  }
  const pvgData = cardData.pvgArr + cardData.pvgDep;
  return (
    <>
      <Card {...props}>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">今日航班总架次</CardTitle>
          <PlaneTakeoff className="text-gray-600 h-5 w-5" />
        </CardHeader>
        <CardContent>
          <div className="text-3xl font-bold">{cardData.total}</div>
          <p className="text-xs text-muted-foreground">比昨日增加5架次</p>
        </CardContent>
      </Card>
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">
            今日浦东进出港架次
          </CardTitle>
          <TowerControl className="text-gray-600 h-5 w-5" />
        </CardHeader>
        <CardContent>
          <div className="text-3xl font-bold">{pvgData}</div>
          <p className="text-xs text-muted-foreground">比昨日增加5架次</p>
        </CardContent>
      </Card>
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">
            本站出港不正常次数
          </CardTitle>
          <ShieldX className="text-gray-600 h-5 w-5" />
        </CardHeader>
        <CardContent>
          <div className="text-3xl font-bold">0</div>
          <p className="text-xs text-muted-foreground">比上月减少1次</p>
        </CardContent>
      </Card>
      <Card>
        <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle className="text-sm font-medium">开口保留故障</CardTitle>
          <ShieldAlert className="text-gray-600 h-5 w-5" />
        </CardHeader>
        <CardContent>
          <div className="text-3xl font-bold">0</div>
          <p className="text-xs text-muted-foreground">比上月增加1个</p>
        </CardContent>
      </Card>
    </>
  );
};

export default HomeCard;
